package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AdSensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {


    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 敏感词列表分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmSensitiveDto dto) {
        //检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //分页参数检查
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());

        //条件构造器
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper();
        //按照敏感词名称模糊查询
        lambdaQueryWrapper.like(dto.getName() != null, WmSensitive::getSensitives, dto.getName());
        //查询需要按照创建时间倒序查询
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        //分页查询
        wmSensitiveMapper.selectPage(page, lambdaQueryWrapper);

        //返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delSensitive(Integer id) {
        //1.检查参数
        if (id == null || id == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询数据
        WmSensitive wmSensitive = wmSensitiveMapper.selectById(id);
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 新增敏感词
     *
     * @param adSensitiveDto
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitiveDto adSensitiveDto) {

        //检查参数
        if (adSensitiveDto == null || StringUtils.isBlank(adSensitiveDto.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //已存在的敏感词则不能保存
        WmSensitive wmSensitive = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, adSensitiveDto.getSensitives()));
        if (wmSensitive != null) {
            //敏感词已存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmSensitive = new WmSensitive(adSensitiveDto.getSensitives(), new Date());
        //保存数据
        wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param adSensitiveDto
     * @return
     */
    @Override
    public ResponseResult upDateSensitive(AdSensitiveDto adSensitiveDto) {
        //检查参数
        if (adSensitiveDto == null || adSensitiveDto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //数据是否存在
        WmSensitive wmSensitive = wmSensitiveMapper.selectById(adSensitiveDto.getId());
        if (wmSensitive == null) {
            //数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmSensitive.setSensitives(adSensitiveDto.getSensitives());
        wmSensitive.setCreatedTime(new Date());
        //保存数据
        wmSensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询所有敏感词
     *
     * @return
     */
    @Override
    public ResponseResult findList() {
        //获取所有的敏感词
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(WmSensitive::getSensitives);
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(lambdaQueryWrapper);
        return ResponseResult.okResult(wmSensitives);
    }
}
